package com.spa.common.cofing;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import cn.dev33.satoken.interceptor.SaRouteInterceptor;
import cn.dev33.satoken.router.SaRouter;
import cn.dev33.satoken.stp.StpUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @version v1.0
 * @ProjectName: revolve-collector-terminal
 * @ClassName: WebMvcConfigurer
 * @Description: 配置静态变量访问、过滤器
 * @Author: P0152257
 * @Date: 7/29/2020 2:28 PM
 */
@Configuration
@Slf4j
public class WebMvcConfigurer extends WebMvcConfigurationSupport {

//    private final AntPathMatcher antPathMatcher = new AntPathMatcher();

//    @Resource
//    private RolePermissionsService rolePermissionsService;

	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {

		registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
		registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
		registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
		super.addResourceHandlers(registry);
	}

	// 放行路径
	private static final String[] EXCLUDE_PATH_PATTERNS = {
			// Swagger
			"**/swagger-ui.html", "/swagger-resources/**", "/null/swagger-resources/**", "/webjars/**", "/v2/**",
			"/swagger-ui.html/**", "/doc.html/**", "/error", "/favicon.ico", "sso/auth", "/images/**","/csrf",
			"/configuration/**",
			// sys登录
			"/sys/login",
			// api登录
			"/api/token"};
	/**
	 * 拦截器
	 */
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		registry.addInterceptor(new SaRouteInterceptor((request, response, handler) ->
		/*
		 * 参数1 拦截路径 参数2 放行路径集合 参数3 回调方法
		 */
//		SaRouter.match("/**").notMatch("/**").check(r -> {
		SaRouter.match("/**").notMatch(EXCLUDE_PATH_PATTERNS).check(r -> {

			log.info(request.getRequestPath());
			// 验证登录
			boolean login = StpUtil.isLogin();
			try {
				StpUtil.checkActivityTimeout();
				StpUtil.updateLastActivityToNow();
			} catch (Exception e) {
				StpUtil.logout(login);
			}

			// 验证token是否非法
//			StpUtil.checkLogin();

//                //验证权限 todo
//                if(login){
//                    String url = URLUtil.getPath(request.getRequestPath());
//                    String routerList = rolePermissionsService.getRouterList();
//                    List<LinkedHashMap<String,String>> resourcesList = JackJsonUtil.fromJson(routerList, ArrayList.class);
//                    for (int i = 0; i < resourcesList.size(); i++) {
//                        LinkedHashMap<String, String> resources = resourcesList.get(i);
//                        // 如果URL匹配成功就不循环了，直接退出循环
//                        // 如果匹配不成功就匹配 /** URL路径了
//                        if (antPathMatcher.match(resources.get("resPath"), url) && request.getMethod().equalsIgnoreCase(resources.get("httpMethod"))) {
//                            List<String> resPath = Collections.singletonList(resources.get("resPath"));
//                            SaRouter.match(resPath).check(r1 ->
//                            StpUtil.checkPermission(resources.get("resCode"))
//                            );
//                            break;
//                        }
//                    }
//                }
		})

		)).addPathPatterns("/**");

	}

}
